Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Equality Checks

Kotlin uses == for structural comparison and === for referential comparison.

More precisely, a == b compiles down to if (a == null) b == null else a.equals(b).

fun main() {

  val authors = setOf("Shakespeare", "Hemingway", "Twain")
  val writers = setOf("Twain", "Shakespeare", "Hemingway")

  println(authors == writers)   // 1
  println(authors === writers)  // 2
}
  1. Returns true because it calls authors.equals(writers) and sets ignore element order.
  2. Returns false because authors and writers are distinct references.